#include "HotKeyEditor.h"

HotKeyEditor::HotKeyEditor(QWidget *parent)
    :QKeySequenceEdit(parent)
{

}

HotKeyEditor::~HotKeyEditor()
{

}

void HotKeyEditor::keyPressEvent(QKeyEvent *pEvent)
{
    QKeySequenceEdit::keyPressEvent(pEvent);
    QString keyStr = keySequence().toString().split(", ").first();
    const char* invalidKeys = "~!@#$%^&*()_+{}|:\"<>?";
    const char* replaceKeys = "`1234567890-=[]\\;\',./";
    const char* key = invalidKeys;
    while(*key)
    {
        if (keyStr.startsWith(*key))
        {
            keyStr.replace(0,1,*(replaceKeys + (key - invalidKeys)));
            break;
        }
        ++key;
    }
    fprintf(stderr, "set shortkey:%s\n", keyStr.toUtf8().data());
    QKeySequence seq(QKeySequence::fromString(keyStr));
    setKeySequence(seq);
    emit keySequenceChanged(seq);
    emit editingFinished();
    fprintf(stderr, "KeySequence:%s,%s\n", seq.toString(QKeySequence::PortableText).toUtf8().data(), seq.toString(QKeySequence::NativeText).toUtf8().data());
}
